#include "sqlquery.h"

SqlQuery::SqlQuery(const QString & query, QObject* parent) : QObject(parent) 
{
	active = false;
	currentQuery = query;
}

SqlQuery::SqlQuery(QObject* parent): QObject(parent)
{
	active = false;
}

SqlQuery::~SqlQuery()
{
	
}

int SqlQuery::size() const {
	if (isActive()) {
		return queryResult.size();
	}
	return -1;
}

bool SqlQuery::exec(const QString & query ) 
{
	currentQuery = query;
	return this->exec();
}

bool SqlQuery::exec() 
{
	if (currentQuery != QString("")) {
		ServerAdapter * adapter = ServerAdapter::serverAdapter();
		/*if (*/adapter->query(currentQuery, queryResult, order); /*) {
			return false;
		}*/
		
		iterator = new QListIterator<QVariant>(queryResult);
		active = true;
		return true;
	}

	return false;
}

bool SqlQuery::next()
{
	if (isActive()) {
		if (iterator->hasNext()) {
			iterator->next();
			return true;
		}
	}
	return false;

}

bool SqlQuery::previous()
{
	if (isActive()) {
		if (iterator->hasPrevious()) {
			iterator->previous();
			return true;
		}
	}
	return false;
}

QVariant SqlQuery::value(QString index) 
{
	if (isActive()) {
		if (iterator->hasPrevious()) {
			if (iterator->peekPrevious().toMap().contains(index)) {
				return iterator->peekPrevious().toMap()[index];
			}
		}
		return QVariant();
	}
	return QVariant();
}

bool SqlQuery::first()
{
	if (isActive()) {
		iterator->toFront();
		return true;
	}
	return false;
}

	
bool SqlQuery::last()
{
	if (isActive()) {
		iterator->toBack();
		return true;
	}
	return false;
}

QVariant SqlQuery::value(int col)
{
	if (isActive()) {
		return value(order.key(col));
	}
	return QVariant();
}

bool SqlQuery::isActive() const 
{
	return active;		
}

bool SqlQuery::isSelect() const
{
	return this->currentQuery.contains("SELECT");
}

int SqlQuery::columnCount() const
{
	if (isActive()) {
		if (iterator->hasNext()) {
			return iterator->peekNext().toMap().size() ;
		}
		else if (iterator->hasPrevious()) {
			return iterator->peekPrevious().toMap().size() ;
		}
		else {
			return 0;
		}
	}
	return -1;
}

QList<QString> SqlQuery::columnNames() const 
{
	QMap<QString,QVariant> * record;
	if (isActive() ) {
		return order.keys();
	}
	else {
		return QList<QString>();
	}
}

//int SqlQuery::rowCount() const
//{
//	if (isActive) {
//		return queryResult.size();
//	}
//	return -1;
//}


QVariant SqlQuery::item(int row, int col) const
{
	QListIterator<QVariant> it(queryResult);
	int currentRow = -1;
	while (it.hasNext() && currentRow != row) {
		it.next();
		currentRow++;
	}
	if (currentRow != row) {
		return QVariant();
	}
	
	if (columnCount() > col) {
		return it.peekPrevious().toMap()[order.key(col)];
	}
		
	return QVariant();
}

SqlQuery & SqlQuery::operator=(SqlQuery & other) {

	this->queryResult = other.queryResult;
	this->active = other.active;
	this->currentQuery = other.currentQuery;
	if (this->queryResult.size() != 0) {
		iterator = new QListIterator<QVariant>(queryResult);
	}

	return *this;
}